<% layout('/layouts/default.html', {title: '到单押汇管理', libs: ['validate','fileupload','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(billNegotiated.isNewRecord ? '新增到单押汇' : '编辑到单押汇')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${billNegotiated}" action="${ctx}/financial/billnegotiated/billNegotiated/creatbillNegotiatedList" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('客户名称')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="customerId" blankOption="true"
								items="${cuList}"
								itemLabel="customerName" itemValue="customerId" onchange="cusChange()"
								class="form-control required"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('服务订单编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="serviceOrderCode"
								onchange="orderChange()"
								blankOption="true" class="form-control" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('国内资方')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="investorId"  maxlength="100" class="form-control" readonly="true"  onchange="inChange()"/>
								<input type="hidden" name="investorId" value="">
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('信用证号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="letterCreditCode" maxlength="100" class="form-control" readonly="true"/>
								<input type="hidden" name="letterCreditCode" value="">
							</div>
						</div>
					</div>
				</div>
<!--				<div class="box-footer">-->
<!--					<div class="row">-->
<!--						<div class="col-sm-offset-2 col-sm-10">-->
<!--							<% if (hasPermi('financial:billnegotiated:billNegotiated:edit')){ %>-->
<!--							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('查询')}</button>&nbsp;-->
<!--							<% } %>-->
<!--							<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('重置')}</button>-->
<!--						</div>-->
<!--					</div>-->
<!--				</div>-->
<!--				<div class="form-group">-->
<!--					<button type="submit" class="btn btn-primary btn-sm">${text('查询')}</button>-->
<!--					<button type="reset" class="btn btn-default btn-sm">${text('重置')}</button>-->
<!--				</div>-->
				<h4 class="form-unit">${text('车辆信息')}</h4>
				<table id="billNegotiatedDetailDataGrid1"></table>
<!--				<% if (hasPermi('financing:finserviceorder:finServiceOrder:edit')){ %>-->
<!--				&lt;!&ndash;						<a href="#" id="tfinancingServiceOrderDetailDataGridAddRowBtn1" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>&ndash;&gt;-->
<!--				<% } %>-->

				<h4 class="form-unit">${text('押汇日期信息')}</h4>
				<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required">*</span> ${text('开始日期')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="startDate" readonly="true"  maxlength="20" class="form-control laydate "
							dataFormat="date" data-type="date" data-format="yyyy-MM-dd"  data-done="dateChange()" readonly="true"/>
							<#form:hidden path="area"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required">*</span> ${text('资方结束日期')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="investorEndDate" readonly="true"  maxlength="20" class="form-control laydate"
							dataFormat="date" data-type="date" data-format="yyyy-MM-dd"  readonly="true" data-done="checkInvEndDate()"/>
<!--							<input type="hidden" name="investorEndDate" value="">-->
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required">*</span> ${text('是否N+1')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:select path="n1Flag" dictType="sys_yes_no" blankOption="true" class="form-control" />
							<#form:hidden path="area"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required">*</span> ${text('客户结束日期')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="customerEndDate" readonly="true"  maxlength="20" class="form-control laydate "
							dataFormat="date" data-type="date" data-format="yyyy-MM-dd" readonly="true" data-done="checkCusEndDate()"/>
<!--							<input type="hidden" name="customerEndDate" value="">-->
						</div>
					</div>
				</div>

				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('资方押汇方案')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:select path="customerRealExchangeRate" dictType="" blankOption="true" class="form-control" onchange="dateChange()"/>
							<#form:hidden path="area"/>
						</div>
					</div>
				</div>
				</div>
<!--				<div style="width:50%;height:130px;"></div>-->
				<div class="form-unit">${text('附件信息')}</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2">
								<span class="required hide">*</span> ${text('附件上传')}：</label>
							<div class="col-sm-10">
								<#form:fileupload id="uploadFile" bizKey="${billNegotiated.id}" bizType="billNegotiated_file"
								uploadType="all" class="" readonly="false" preview="true"/>
							</div>
						</div>
					</div>
				</div>
				<div style="padding-left: 500px;padding-top: 20px;" class="row">
				<% if (hasPermi('financial:billnegotiated:billNegotiated:edit')){ %>
				<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('确认生成')}</button>&nbsp;
				<% } %>
				<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
				</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
	function checkInvEndDate(){
		let startDate=$("#startDate").val();
		let endDate=$("#investorEndDate").val();
		if(endDate<=startDate){
			$("#endDate").val('');
			js.showErrorMessage("资方结束日期不能早于开始日期！");
		}
	}
	function checkCusEndDate(){
		let startDate=$("#startDate").val();
		let endDate=$("#customerEndDate").val();
		if(endDate<=startDate){
			$("#endDate").val('');
			js.showErrorMessage("客户结束日期不能早于开始日期！");
		}
	}
//初始化到单押汇明细DataGrid对象
$("#billNegotiatedDetailDataGrid1").dataGrid({

	data: ${toJson(billNegotiated.billNegotiatedDetailList1)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度

	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'类型', name:'type', editable:true, hidden:true},
		{header:'主键', name:'billNegotiatedDetailId', editable:true, hidden:true},
		{header:'${text("勾选")}', name:'taxClearanceId', width:15, editable:true, edittype:'checkbox'},
		{header:'${text("车架号")}', name:'frameNumber', width:100, editable:true, edittype:'text', editoptions:{'maxlength':'20', 'class':'form-control', 'readonly':'true'}},
		{header:'${text("车辆名称")}', name:'carName', width:100, editable:true, edittype:'text', editoptions:{'class':'form-control', 'readonly':'true'}},
		{header:'${text("品牌")}', name:'brand', width:100, editable:true, edittype:'select', editoptions:{'maxlength':'20', 'class':'form-control', 'readonly':'true',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('car_pinpai')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue',
			}},
		// {header:'${text("资方-押汇利息")}', name:'negotiatedInterest', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number', 'readonly':'true'}},
		// {header:'${text("应还款金额")}', name:'repaymentAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number', 'readonly':'true'}},
		{header:'${text("车型")}', name:'model', width:100, editable:true, edittype:'text', editoptions:{'maxlength':'255', 'class':'form-control','readonly':'true'}},
		{header:'${text("提单号")}', name:'ladingBillId', width:100, editable:true, edittype:'text', editoptions:{'maxlength':'20', 'class':'form-control', 'readonly':'true'}},
		{header:'${text("已押汇")}', name:'bill', editable:true, hidden:true},
		// {header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
		// 	var actions = [];
		// 	if (val == 'new'){
		// 		actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#billNegotiatedDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
		// 	}else{
		// 		actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#billNegotiatedDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
		// 	}
		// 	return actions.join('');
		// }, editoptions: {defaultValue: 'new'}}
	],

	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#billNegotiatedDetailDataGridAddRowBtn1'),	// 子表增行按钮
	editGridAddRowInitData: {billNegotiatedDetailId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

	// 编辑表格的提交数据参数
	editGridInputFormListName: 'billNegotiatedDetailList3', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,taxClearanceId,frameNumber,brand,model,', // 提交数据列表的属性字段

	// 加载成功后执行事件
	ajaxSuccess: function(data){

	}
});

</script>
<script>
	var tax;
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});




$(function () {
	if (typeof window.webuploaderRefresh == 'function'){
		window.webuploaderRefresh();
	}
})
function orderChange() {

		$.ajax({
			type: "POST",
			url: "${ctx}/financial/billnegotiated/billNegotiated/getOrderById",
			data: {"serviceOrderCode": $("#serviceOrderCode").val()},
			success: function (data) {
				var option1 = $("<option>").attr({
					"value" : data.investor.supplierId
				}).text(data.investor.supplierName);
				var option2 = $("<option>").attr({
					"value" : data.letterCredit.letterCreditId
				}).text(data.letterCredit.letterCreditCode);
				$("#letterCreditCode").empty();
				$("#investorId").empty();
				$("#letterCreditCode").append(option2);
				$("#letterCreditCode option:first").prop("selected", 'selected');
				$("#letterCreditCode").change();
				$("input[name='letterCreditCode']").val(data.letterCredit.letterCreditId);
				// $('#investorId').val(data.supplier.supplierName).trigger('change');
				$("#investorId").append(option1);
				$("#investorId option:first").prop("selected", 'selected');
				$("#investorId").change();
				$("input[name='investorId']").val(data.investor.supplierId);
				// $('#letterCreditCode').val(data.letterCredit.letterCreditCode).trigger('change');
				// $('#supplier').select2('val', data.supplier.supplierName);
				// $('#letterCredit').select2('val', data.letterCredit.letterCreditCode);
				var list=data.serviceOrderDetailList;
				var ids1 = $("#billNegotiatedDetailDataGrid1").getDataIDs();
				for (var i = 0; i < ids1.length ; i++) {
					$('#billNegotiatedDetailDataGrid1').dataGrid('delRowData', ids1[i]);
				}
				for(let i=0;i<list.length;i++) {
					$("#billNegotiatedDetailDataGrid1").jqGrid("addRow", {
						position: "last",
						addRowParams: {keys: false, focusField: true},
						initdata: {
							frameNumber: list[i].frameNumber,
							carName: list[i].tcar.carName,
							brand: list[i].brand,
							model: list[i].model,
							ladingBillId:list[i].ladingBill.ladingBillCode,
						}
					});
				}
				if (typeof window.webuploaderRefresh == 'function'){
					window.webuploaderRefresh();
				}
				$("input[name=\"ladingBillId\"]").each(function () {

					if($(this).val()=="未生成提单"){
						$(this).parent().parent().find("input[type=\"checkbox\"]").prop('disabled',true);
					}


				})

			}
		});



}

function cusChange() {

	$.ajax({
		type: "POST",
		url: "${ctx}/financial/billnegotiated/billNegotiated/getOrderByCuId",
		data: {"customerId": $("#customerId").val()},
		success: function (data) {
			// $('#supplier').select2('val', data.supplier.supplierName);
			// $('#letterCredit').select2('val', data.letterCredit.letterCreditCode);
			$("#serviceOrderCode").empty();
			if (data.length > 0) {
				for (var i = 0; i < data.length; i++) {
					var orderId = data[i].serviceOrderId;
					var orderCode = data[i].serviceOrderCode;
					var option = $("<option>").attr({
						"value" : orderId
					}).text(orderCode);
					$("#serviceOrderCode").append(option);
				}
				$("#serviceOrderCode option:first").prop("selected", 'selected');
				$("#serviceOrderCode").change();
			} else{
				js.showErrorMessage("该客户没有订单信息！");
			}

		}
	});

}
$(function () {
	$("#btnSubmit").click(function(){
		//复选框的name='od'
		$("checkbox[name='od']").each(function(){
			if($(this).attr("checked")== 'on'){
				//是选中
			}else{
				//       td       tr
				$(this).parent().parent().find("input").removeAttr("name");
			}
		});
	});

})
//资方选择后，给押汇方案赋值
function inChange() {

	$.ajax({
		type: "POST",
		url: "${ctx}/financial/billnegotiated/billNegotiated/getFinancialInfoByInId",
		data: {"investorId": $("#investorId").val()},
		success: function (data) {
			// $('#supplier').select2('val', data.supplier.supplierName);
			// $('#letterCredit').select2('val', data.letterCredit.letterCreditCode);
			tax=data;
			$("#customerRealExchangeRate").empty();
			if (data.length > 0) {
				for (var i = 0; i < data.length; i++) {
					var groupName = data[i].groupName;
					var option = $("<option>").attr({
						"value" : groupName
					}).text(groupName);
					$("#customerRealExchangeRate").append(option);
				}
				$("#customerRealExchangeRate option:first").prop("selected", 'selected');
				$("#customerRealExchangeRate").change();
			} else{
				js.showErrorMessage("该资方没有押汇方案！");
			}

		}
	});

}

//资方选择后，给押汇方案赋值
function dateChange() {
	if ( $("#startDate").val()!=""&&$("#customerRealExchangeRate").val()!="") {
		$.ajax({
		type: "POST",
		url: "${ctx}/financial/billnegotiated/billNegotiated/getPadTaxInterestByInId",
		data: {
			"investorId": $("#investorId").val(),
			"groupName": $("#customerRealExchangeRate").val(),
		},
		success: function (data) {
			// $('#supplier').select2('val', data.supplier.supplierName);
			// $('#letterCredit').select2('val', data.letterCredit.letterCreditCode);
            var startDate=$("#startDate").val();
			$("#investorEndDate").val(js.formatDate(js.addDate(js.parseDate(startDate),
					(data[0])[0].days), 'yyyy-MM-dd'));
			$("#customerEndDate").val(js.formatDate(js.addDate(js.parseDate(startDate),
					(data[1])[0].days), 'yyyy-MM-dd'));

		}
	});
}

}


</script>
